package com.situ.vivamarket.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.situ.vivamarket.dao.GoodsDao;
import com.situ.vivamarket.model.Goods;
import com.situ.vivamarket.model.search.GoodsSearchBean;
import com.situ.vivamarket.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    private GoodsDao goodsDao;
    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    @Override
    public List<Goods> getAllGoods(GoodsSearchBean gsb, Page<?> p) {
        try(Page<?>__= PageHelper.startPage(p.getPageNum(),p.getPageSize())){
            return goodsDao.findAll(gsb);
        }
    }

    @Override
    public int deleteByIds(Integer[] ids) {
        return goodsDao.deleteByIds(ids);
    }

    @Override
    public boolean add(Goods goods) {
        goods.setCreateTime(LocalDateTime.now());
        goods.setLastModifyTime(LocalDateTime.now());
        return goodsDao.insert(goods)>0;
    }

    @Override
    public Goods findById(Integer id) {
        return goodsDao.findById(id);
    }

    @Override
    public boolean update(Goods goods) {
        goods.setLastModifyTime(LocalDateTime.now());
        return goodsDao.update(goods)>0;
    }
//  根据类别查找。限制个数，用于前端比较
    @Override
    public List<Goods> findByCatId(Integer catId, int limit) {
        return goodsDao.findByCatId(catId, limit);
    }
}
